<template>
	<view class="detail">
		<!-- 面试题详情页 -->
		<view class="title detail-header">
			<view class="left">题目</view>
			<view class="right">
				<text>收藏</text>
			</view>
		</view>

		<view class="detail-content">
			<!-- 滑块容器 -->
			<swiper :current="current" @change="changeFn">
				<swiper-item v-for="item in arr" :key="item._id">
					<view class="swiper-item">
						<view class="wrapper">
							<view class="tit" style="margin-bottom: 20rpx;">
								{{item.question}}
							</view>

							<view class="box" v-if="item.picUrl">
								<image @click="previewImage(item.picUrl)" :src="item.picUrl" mode="widthFix"></image>
							</view>

							<view class="box" v-if="item.answers">
								<view class="p" v-for="(opt,i) in item.answers" :key="i">
									{{i + 1}}. {{opt}}
								</view>
							</view>
							<view class="box" v-else>
								<view class="p">{{item.answer}}</view>
							</view>

						</view>
					</view>
				</swiper-item>
			</swiper>
		</view>

		<view class="detail-footer">
			<div class="footer-item"><text @click="prevFn">上一题</text></div>
			<div class="footer-item"><text>{{current + 1}}/ {{arr.length}}</text></div>
			<div class="footer-item"><text @click="nextFn">下一题</text></div>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				arr: [],
				current: 0,

			}
		},
		methods: {
			previewImage(imageUrl) {
				uni.previewImage({
					current: imageUrl, // 当前显示图片的http链接
					urls: [imageUrl] // 需要预览的图片http链接列表
				});
			},
			prevFn() {
				let num = this.current
				num--
				if (num < 0) {
					num = 0;
					// 提示
					uni.showToast({
						title: "没有上一题了",
						icon: "none"
					})
				}
				this.current = num;
			},
			nextFn() {
				let num = this.current
				num++
				if (num > (this.arr.length - 1)) {
					num = this.arr.length - 1;
					// 提示
					uni.showToast({
						title: "没有下一题了",
						icon: "none"
					})
				}
				this.current = num;
			},
			changeFn(event) {
				// console.log(event.detail.current)
				// 赋值
				this.current = event.detail.current;
			}

		},
		onLoad(options) {
			if (options._id) {
				this._id = options._id;
				// 从本地取值
				let data = uni.getStorageSync("INTERVIEW");
				// console.log(data);
				if (data) {
					let newData = data.map(item => {
						if (item.answer && item.answer.includes('\n')) {
							item.answers = item.answer.split('\n')
						}
						return item;
					})
					// 赋值
					this.arr = newData;
					// 循环数组
					for (let i = 0; i < data.length; i++) {
						if (data[i]._id == options._id) {
							// 赋值给current变量
							this.current = i;
							break;
						}
					}
				}
			}
		}
	}
</script>



<style lang="scss">
	@import "../../static/scss/variable.scss";

	.detail {
		padding: $padd;

		swiper {
			height: 1000rpx;
		}

		.wrapper {
			background-color: #f0f0f0;
			padding: 10rpx 20rpx;
			height: 1000rpx;
			overflow: scroll;
		}
	}

	.detail-footer {
		padding: $padd;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 100;
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		box-shadow: 0px -1px 2px #f0f0f0;
	}

	.detail-content {
		padding-bottom: 100rpx;

		.tit {
			padding: 10rpx 0rpx;
			font-size: 36rpx;
			position: relative;
			padding-left: 20rpx;
			font-weight: bold;

			&::before {
				content: "";
				position: absolute;
				left: 0;
				top: 20rpx;
				width: 6rpx;
				height: 40rpx;
				background-color: $theme-color;
			}
		}

		.box {
			padding: 10rpx 20rpx;
			background-color: #fff;
			box-sizing: border-box;

			// margin-top: 16rpx;
			image {
				width: 100%;
				display: block
			}

			.p {
				padding: 10rpx 0rpx;
				border-bottom: 1px dashed #f0f0f0;
			}
		}

		.img {
			width: 100%;
		}
	}

	.detail-header {
		display: flex;
		width: 100%;
		box-sizing: border-box;
		justify-content: space-between;

		.right {
			font-weight: normal;
			font-size: 32rpx;
		}
	}
</style>